﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using MiniErp.Model.Entities;
using MiniErp.Model.Entities.AssetManagement;
using MiniErp.Mysql.Log;

namespace MiniErp.Mysql.Data
{

    public class MysqlContext : DbContext
    {
        public MysqlContext(DbContextOptions<MysqlContext> options) : base(options) { }

        public DbSet<UserInfoEntity> UserInfos { get; set; }
        public DbSet<AssetInfoEntity> AssetInfos { get; set; }
        public DbSet<AssetStateEntity> AssetStates { get; set; }
        public DbSet<GeneralTypeEntity> GeneralTypes { get; set; }
        public DbSet<SecondaryTypeEntity> SecondaryTypes { get; set; }
        public DbSet<DepartmentEntity> Departments { get; set; }
        public DbSet<PositionEntity> Positions { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserInfoEntity>();
            modelBuilder.Entity<AssetStateEntity>();
            modelBuilder.Entity<GeneralTypeEntity>();
            modelBuilder.Entity<SecondaryTypeEntity>();
            modelBuilder.Entity<DepartmentEntity>();
            modelBuilder.Entity<PositionEntity>();
            //组合主键
            modelBuilder.Entity<AssetInfoEntity>()
                .HasKey(item => new { item.AId, item.GeneralTypeId, item.SecondaryTypeId });
        }
#if DEBUG
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var loggerFactory = new LoggerFactory();
            loggerFactory.AddProvider(new EFCoreLoggerProvider());
            //添加EnableSensitiveDataLogging，启用敏感数据记录
            optionsBuilder.UseLoggerFactory(loggerFactory).EnableSensitiveDataLogging();
        }
#endif
    }

}
